package com.whq.mvc.base.controller;

import com.whq.mvc.base.redis.RedissonUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/test")
public class TestController {

    final static Logger logger = LoggerFactory.getLogger(TestController.class);

    @GetMapping("/{id}")
    public String test(@PathVariable(value = "id") int id) {
        logger.info("参数：{},线程id：{}", id, Thread.currentThread().getId());
        RedissonClient client = RedissonUtils.getInstance().getRedisson("127.0.0.1", "6379");
        RLock lock = client.getLock("myLock");
        lock.lock(20, TimeUnit.SECONDS);
        try {
            logger.info("{}开始处理业务", Thread.currentThread().getId());
            TimeUnit.SECONDS.sleep(10);
            logger.info("{}处理业务完成", Thread.currentThread().getId());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
        return String.valueOf(id);
    }
}
